package com.yds.xutils_facrotypattern.https;

import com.google.gson.Gson;
import com.yds.xutils_facrotypattern.MyApplication;

import org.xutils.common.Callback;
import org.xutils.http.HttpMethod;
import org.xutils.http.RequestParams;
import org.xutils.x;

/**
 * @author YDS
 * @date 2021/5/25
 * @discribe xutils http request restful
 */
@SuppressWarnings("all")
public class XutilsHttpRestfulEngine extends XutilsHttpBaseEngine implements IHttpsMethodListener {

    @Override
    public void httpsGet(RequestParams params,
                         HttpRequestCallBack callBack) {
        boolean networkAvailable = NetworkUtil.isNetworkAvailable(MyApplication.getApplication());
        if (!networkAvailable) {
            return;
        }

        addDefaultHeader(params);
        x.http().request(HttpMethod.GET, params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                callBack.onSuccess(result);
                printRequestLog(params, result);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                initResultCodeRestful(params, ex, callBack);
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {
                callBack.onFinish();
            }
        });
    }

    @Override
    public void httpsPost(RequestParams params,
                          HttpRequestCallBack callBack) {
        boolean networkAvailable = NetworkUtil.isNetworkAvailable(MyApplication.getApplication());
        if (!networkAvailable) {
            return;
        }

        addDefaultHeader(params);
        x.http().request(HttpMethod.POST, params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
//                Log.i("X-utils","onSuccess = "+result);
                callBack.onSuccess(result);
                printRequestLog(params, result);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                initResultCodeRestful(params, ex, callBack);
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {
                callBack.onFinish();
            }
        });
    }

    @Override
    public void httpsPostAsJson(RequestParams params, Object obj, HttpRequestCallBack callBack) {
        boolean networkAvailable = NetworkUtil.isNetworkAvailable(MyApplication.getApplication());
        if (!networkAvailable) {
            return;
        }

        params.setAsJsonContent(true);
        Gson gson = new Gson();
        String json = gson.toJson(obj);
        if (json.contains("\\")) {
            json = json.replace("\\", "");
        }
        params.setBodyContent(json);

        addDefaultHeader(params);
        x.http().request(HttpMethod.POST, params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                callBack.onSuccess(result);
                printRequestLog(params, result);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                initResultCodeRestful(params, ex, callBack);
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {
                callBack.onFinish();
            }
        });
    }

    @Override
    public void httpsPatch(RequestParams params,
                           HttpRequestCallBack callBack) {
        boolean networkAvailable = NetworkUtil.isNetworkAvailable(MyApplication.getApplication());
        if (!networkAvailable) {
            return;
        }

        addDefaultHeader(params);
        x.http().request(HttpMethod.PATCH, params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                callBack.onSuccess(result);
                printRequestLog(params, result);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                initResultCodeRestful(params, ex, callBack);
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {
                callBack.onFinish();
            }
        });
    }

    @Override
    public void httpsRequestCancel() {

    }

    @Override
    public void httpsDelete(RequestParams params, HttpRequestCallBack callBack) {
        boolean networkAvailable = NetworkUtil.isNetworkAvailable(MyApplication.getApplication());
        if (!networkAvailable) {
            return;
        }

        addDefaultHeader(params);
        x.http().request(HttpMethod.DELETE, params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                callBack.onSuccess(result);
                printRequestLog(params, result);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                initResultCodeRestful(params, ex, callBack);
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {
                callBack.onFinish();
            }
        });
    }
}
